package com.example.blogdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blogdemo.common.Utils.BlogBeanUtils;
import com.example.blogdemo.common.exception.BlogException;
import com.example.blogdemo.mapper.BlogMapper;
import com.example.blogdemo.pojo.dataobject.BlogInfo;
import com.example.blogdemo.pojo.request.AddRequest;
import com.example.blogdemo.pojo.request.UpdateBlogRequest;
import com.example.blogdemo.pojo.respond.BlogRespond;
import com.example.blogdemo.pojo.respond.BlogRespondOne;
import com.example.blogdemo.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

//import static java.util.stream.Nodes.collect;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;



    @Override
    public List<BlogRespond> getList() {

        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0);

        BlogRespond blogRespond = new BlogRespond();

        List<BlogInfo> blogInfos = blogMapper.selectList(queryWrapper);
        List<BlogRespond> blogResponds = blogInfos.stream().map(x -> BlogBeanUtils.transResponds(x)).collect(Collectors.toList());

        return blogResponds;

    }


    @Override
    public BlogRespondOne getBlog(Integer p) {


        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0)
                .eq(BlogInfo::getId, p);
//        BlogInfo blogInfo = blogMapper.selectById(p);
        BlogInfo blogInfo = blogMapper.selectOne(queryWrapper);
        return BlogBeanUtils.transOne(blogInfo);


    }

    @Override
    public boolean addBlgo(AddRequest addRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addRequest, blogInfo);
        int insert = blogMapper.insert(blogInfo);
        if (insert == 1) {
            return true;
        }
        return false;

    }

    @Override
    public Boolean blogDelete(Integer blogId) {

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setDeleteFlag(1);
        blogInfo.setId(blogId);
        try {
            int i = blogMapper.updateById(blogInfo);
            if (i == 1)
                return true;
            else
                return false;


        } catch (Exception e)  {
            log.error("删除博客失败，e:{}", e.getMessage());
            throw new BlogException("删除博客失败");
        }
    }

    @Override
    public Boolean blogUpdata(UpdateBlogRequest updateBlogRequest) {

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(updateBlogRequest.getId());
        blogInfo.setTitle(updateBlogRequest.getTitle());
        blogInfo.setContent(updateBlogRequest.getContent());

        try {
            int i = blogMapper.updateById(blogInfo);
            return i == 1;
        } catch (Exception e) {
            log.error("修改文章失败，e:{}", e.getMessage());
            throw new BlogException("修改文章失败");
        }
    }


}
